package com.scpii.api.controller.user;

import java.util.Date;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scpii.api.common.Constants;
import com.scpii.api.common.base.Response;
import com.scpii.api.common.base.ResponseThreadlocal;
import com.scpii.api.domain.auth.UsersInfoDetails;
import com.scpii.api.domain.user.UserLinks;
import com.scpii.api.service.user.UserlinksService;
import com.scpii.api.util.PrincipalUtil;

@Controller
public class BindController {

	@Resource
	private UserlinksService userLinksService;

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(value = "/user/bind", method = RequestMethod.POST)
	@ResponseBody
	public Boolean bind(@Valid UserLinks userLinks) {
		Response response = ResponseThreadlocal.getResponse();
		String tips = null;
		UserLinks persistUserLinks = userLinksService.selectAppUserLinks(
				PrincipalUtil.getPrincipal().getAppId(),
				userLinks.getProvider(), userLinks.getLoginName());
		if (persistUserLinks != null) {
			persistUserLinks.setProfiles(userLinks.getProfiles());
			persistUserLinks.setToken(userLinks.getToken());
			persistUserLinks.setTokenTimeOut(userLinks.getTokenTimeOut());
			persistUserLinks.setUpdateDt(new Date());
			userLinksService.updateByPrimaryKeySelective(persistUserLinks);
			if (Constants.THIRD_PARTY_LOGIN_TYPES.get("T_SINA").equals(
					persistUserLinks.getProvider())) {
				tips = "该微博号已经被绑定,请使用其他账号绑定";
			} else if (Constants.THIRD_PARTY_LOGIN_TYPES.get("T_TENCENT")
					.equals(persistUserLinks.getProvider())) {
				tips = "该QQ号已经被绑定,请使用其他账号绑定";
			}
			response.setTips(tips);
			return false;
		}
		UserLinks record = new UserLinks();
		record.setUserId(PrincipalUtil.getPrincipal().getId());
		record.setProvider(userLinks.getProvider());
		UserLinks userLinksCk = userLinksService.selectBySelective(record);
		if (userLinksCk != null) {
			tips = "你已绑定此类账号不能绑定";
			response.setTips(tips);
			return false;
		}
		userLinks.setUserId(PrincipalUtil.getPrincipal().getId());
		userLinks.setCreateBy(PrincipalUtil.getPrincipal().getId());
		userLinks.setCreateDt(new Date());
		userLinksService.insertSelective(userLinks);
		return true;
	}

	@PreAuthorize("hasRole('ROLE_USER')")
	@RequestMapping(value = "/user/unbind", method = RequestMethod.POST)
	@ResponseBody
	public Boolean unbind(@RequestParam("provider") String provider) {
		Response response = ResponseThreadlocal.getResponse();
		String tips = null;
		UsersInfoDetails usersInfoDetails = PrincipalUtil.getPrincipal();
		if (StringUtils.equals(usersInfoDetails.getProvider(), provider)) {
			tips = "不能解绑此账号";
			response.setTips(tips);
			return false;
		}
		Long userId = usersInfoDetails.getId();
		UserLinks record = new UserLinks();
		record.setProvider(provider);
		record.setUserId(userId);
		UserLinks persistLinks = userLinksService.selectBySelective(record);
		if (persistLinks == null) {
			tips = "不存在此账号的绑定关系,解绑失败";
			response.setTips(tips);
			return false;
		}
		userLinksService.deleteByPrimaryKey(persistLinks.getId());
		return true;
	}
}
